// Copyright (C) Kumo inc. and its affiliates.
// Author: Jeff.li lijippy@163.com
// All rights reserved.
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published
// by the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program.  If not, see <https://www.gnu.org/licenses/>.
//


#pragma once

#include <pollux/common/base/exceptions.h>
#include <pollux/dwio/common/direct_decoder.h>
#include <pollux/dwio/common/int_decoder.h>
#include <pollux/dwio/dwrf/common/rle_v1.h>
#include <pollux/dwio/dwrf/common/rle_v2.h>

namespace kumo::pollux::dwrf {
/**
 * Create an RLE decoder.
 * @param input the input stream to read from
 * @param version version of RLE decoding to do
 * @param pool memory pool to use for allocation
 */
template <bool isSigned>
std::unique_ptr<dwio::common::IntDecoder<isSigned>> createRleDecoder(
    std::unique_ptr<dwio::common::SeekableInputStream> input,
    RleVersion version,
    memory::MemoryPool& pool,
    bool useVInts,
    uint32_t numBytes) {
  switch (static_cast<int64_t>(version)) {
    case RleVersion_1:
      return std::make_unique<RleDecoderV1<isSigned>>(
          std::move(input), useVInts, numBytes);
    case RleVersion_2:
      return std::make_unique<RleDecoderV2<isSigned>>(std::move(input), pool);
    default:
      POLLUX_UNSUPPORTED("Not supported: {}", static_cast<int64_t>(version));
      return {};
  }
}

/**
 * Create a direct decoder
 */
template <bool isSigned>
std::unique_ptr<dwio::common::IntDecoder<isSigned>> createDirectDecoder(
    std::unique_ptr<dwio::common::SeekableInputStream> input,
    bool useVInts,
    uint32_t numBytes) {
  return std::make_unique<dwio::common::DirectDecoder<isSigned>>(
      std::move(input), useVInts, numBytes);
}

} // namespace kumo::pollux::dwrf
